Skip to content

Improve snowball stemming by adding an insert-only cache - #16356

Open
costin wants to merge 9 commits into
apache:mainfrom
costin:lucene/snowball-stem-cache-pr
Open

Improve snowball stemming by adding an insert-only cache#16356
costin wants to merge 9 commits into
apache:mainfrom
costin:lucene/snowball-stem-cache-pr

Conversation

@costin

@costin costin commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

Snowball stemming is expensive due to algorithmic suffix manipulation repeated identically for every occurrence of the same token.
This PR adds an small insert-only cache (CharArrayMap) to avoid the redundant work by exploiting the fact that natural text is Zipfian (same short words account for majority of token occurrences).
The cache is small (1024 entries, ~75KB) and caches only tokens up to 10 chars.
It is enabled by default to avoid mapping changes for existing users and can be disabled through maxCacheSize=0.

Benchmarks

AMD EPYC 7R32 (c5a.2xlarge), JDK 25, 500 unique words, 10K tokens Zipfian corpus. 3 forks, 10 iters × 1s. cacheSize=0 is the uncached baseline.

language vocabSize cacheSize=0 cacheSize=1024 speedup cacheSize=4096 speedup cacheSize=8192 speedup
English 500 277.9 ± 2.2 751.4 ± 4.0 2.70x 743.4 ± 6.9 2.67x 734.2 ± 7.6 2.64x
English 5,000 1,893 4,421 2.34x 4,526 2.39x 4,521 2.39x
English 50,000 1,882 4,366 2.32x 4,476 2.38x 4,488 2.38x
German 500 201.4 ± 1.3 550.2 ± 9.6 2.73x 555.3 ± 7.2 2.76x 551.7 ± 1.7 2.74x
German 5,000 1,773 4,163 2.35x 4,237 2.39x 4,222 2.38x
German 50,000 1,757 4,071 2.32x 4,159 2.37x 4,154 2.36x

The default cache size of 1024 entries was picked from the sweep above as it captures virtually all the benefits across English and German.

Avoid redundant Snowball stemmer invocations on repeated
tokens via insert-only CharArrayMap cache; 2.3-2.7x faster.
Default 1024 entries (~75 KB), disableable via constructor.
@dweiss

dweiss commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

I don't think it's worth the extra complexity, to be honest. If you have costly components in your indexing pipeline then there's another idea you can try - wrap the costly pipeline into a top-level component which then returns all the cached attributes for a surface token image. The "cache" here can be dumb and just clear itself when it saturates (which is much simpler than any "real" hit-count cache) and just rely on token distribution.

This works for everything (assuming context independance) and indeed can speed up processing... but it is something that users should/can configure themselves, knowing what their data and analysis pipeline is, I think.

@rmuir

rmuir commented Jul 23, 2026

Copy link
Copy Markdown
Member

Also i wonder if the benchmark removed stopwords. They can really skew the results! If you don't want to remove stopwords, you can at least not pass them thru the stemmer, e.g. using KeywordMarkerFilter/StemmerOverrideFilter/ProtectedWordsFilter/etc.

its the same idea: the very high frequency words go into a chararrayset and avoid being run thru the stemmer.

@costin

costin commented Jul 28, 2026

Copy link
Copy Markdown
Contributor Author

Appreciate the feedback.

Regarding the generic wrapping approach, I opted for improving the performance of the existing filters so that users can benefit from it after the upgrade without having to change their setup/mappings.

I reran the benchmark with StopFilter in the pipeline (default English/German stop sets) to see the stopword removal impact:

Stopword removal helps throughput (~50% for English and ~25% for German); the cache gives 2-3x on top of it.

language vocabSize stopFilter cacheSize=0 cacheSize=1024 speedup
English 500 true 409.0 ± 5.4 861.1 ± 4.9 2.11x
English 500 false 278.7 ± 2.2 758.5 ± 1.6 2.72x
English 5,000 true 403.7 ± 2.2 820.7 ± 5.9 2.03x
English 5,000 false 280.3 ± 3.1 673.7 ± 7.0 2.40x
English 50,000 true 389.3 ± 2.1 771.2 ± 2.3 1.98x
English 50,000 false 269.0 ± 10.0 649.0 ± 1.6 2.41x
German 500 true 242.8 ± 4.1 690.8 ± 5.9 2.84x
German 500 false 192.6 ± 3.0 553.0 ± 3.6 2.87x
German 5,000 true 228.5 ± 3.1 624.3 ± 8.1 2.73x
German 5,000 false 184.6 ± 3.5 494.6 ± 5.8 2.68x
German 50,000 true 232.2 ± 3.1 593.7 ± 3.3 2.56x
German 50,000 false 184.2 ± 4.0 464.4 ± 6.1 2.52x

(same hardware and JDK as before)

@rmuir

rmuir commented Jul 28, 2026

Copy link
Copy Markdown
Member

Regarding the generic wrapping approach, I opted for improving the performance of the existing filters so that users can benefit from it after the upgrade without having to change their setup/mappings.

I think this is the concern though. 75KB or N strings is too big by default. analyzer is cached in threadlocal and not just used at indexing time, but also at query time. For users with high query volumes, this cache would just cause problems I think.

users have already complained before about the fact we reuse the termbuffer (termattribute), which is much less memory than this.

Yes, the snowball stemmers are slower but most of our Analyzers don't default to them either... instead they default to lighter and faster approaches. For example, with English even if you dont want a light/minimal approach, you can use the PorterStemmer.java which is much faster than the snowball variant. That's the default used by EnglishAnalyzer. https://github.com/apache/lucene/blob/main/lucene/analysis/common/src/java/org/apache/lucene/analysis/en/EnglishAnalyzer.java#L108

For German, the default is also not snowball, but Savoy's: https://github.com/apache/lucene/blob/main/lucene/analysis/common/src/java/org/apache/lucene/analysis/de/GermanAnalyzer.java#L133

So I'm not sure who the users are that are using English/German snowball filters?

If the user doesn't want to use stopfilter, again, if they can load the stoplist and protect it from being stemmed, they will see a perf improvement, but with much less memory usage and the set doesn't need to be duplicated everywhere but loaded a single time.

Anyway, these are just my thoughts. For both these languages I think we have faster solutions already, and the faster solutions are already the default?

costin added 2 commits July 28, 2026 21:46
Use languages where Snowball is the only stemmer available.
Add numAnalyzers param to simulate multi-index memory pressure.
Sweep cache sizes 64-512 to find the right default.
128 entries (~9KB) gives ~2x throughput while keeping memory bounded.
Diminishing returns past 256 entries.
@costin

costin commented Jul 28, 2026

Copy link
Copy Markdown
Contributor Author

You're right, English and German were the wrong languages to benchmark.
I've changed this to Romanian and Turkish (which has more complicated stemming rules).
From what I can tell, 18 default language analyzers use SnowballFilter without a dedicated, lighter alternative.

I reran the benchmark with the two languages and used different cache sizes for better tuning:

(AMD EPYC, JDK 25, vocab=5000)

language cache stop=true × stop=false ×
Romanian 0 328 ± 2 255 ± 2
Romanian 128 563 ± 4 1.7× 467 ± 3 1.8×
Romanian 256 618 ± 4 1.9× 546 ± 5 2.1×
Turkish 0 178 ± 1 115 ± 0
Turkish 128 356 ± 1 2.0× 262 ± 1 2.3×
Turkish 256 430 ± 3 2.4× 324 ± 1 2.8×

the sweet spot looks to be between 128-256. 128 entries take around ~9KB (capped) for ~2x better stemming so made that the default.

if they can load the stoplist and protect it from being stemmed, they will see a perf
improvement, but with much less memory usage

Agreed this works and it's a good complementary technique.
The cache gives ~2x regardless of whether stop filter is in the pipeline, so it helps even for analyzers that already remove stop words.

@rmuir

rmuir commented Jul 28, 2026

Copy link
Copy Markdown
Member

but its just filling tokens until its full right? So if your first document has a distribution than all the others then it will just perform badly?

Wouldn't it be more effective if cache used LRU-type behavior so that it retained the most frequent inputs?

AFAIK the CharArrayMap doesn't support such semantics (e.g. removeEldestEntry) or anything. Maybe I am missing something.

Cache is allocated lazily on first cacheable token. When full, clears
and refills from subsequent tokens to adapt to distribution changes
since CharArrayMap does not support removal/LRU. Updated benchmark
to process corpus as multiple documents matching real indexing.
@costin

costin commented Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

Good point.
CharArrayMap doesn't support removal so proper LRU isn't possible without a custom data structure. Instead I changed the cache to clear-and-refill when full so it adapts to the current distribution rather than being stuck with early entries.
Kept the 128 default.
Also made allocation lazy so the cache is only created when the first
token is actually stemmed, so analyzers used for query parsing pay zero
memory.

Updated benchmark results:

language cache stop=true × stop=false ×
Romanian 0 323 ± 2 266 ± 2
Romanian 128 466 ± 3 1.4× 352 ± 3 1.3×
Romanian 256 546 ± 4 1.7× 442 ± 5 1.7×
Turkish 0 186 ± 1 115 ± 0
Turkish 128 269 ± 1 1.4× 184 ± 1 1.6×
Turkish 256 276 ± 3 1.5× 230 ± 1 2.0×

@rmuir

rmuir commented Jul 29, 2026

Copy link
Copy Markdown
Member

Also made allocation lazy so the cache is only created when the first token is actually stemmed, so analyzers used for query parsing pay zero memory.

I don't understand this, usually analyzers stem at query-time too.

@costin

costin commented Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

Sorry, you're right. I was thinking of analyzers that haven't processed any tokens yet (such as unused indices).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants